{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "661e4571",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1.函数的定义 ： def\n",
    ">函数名的定义规则-->变量名一样的规则\n",
    ">调用函数的方式\n",
    "\n",
    ">参数：\n",
    ">>形式参数（定义函数）\n",
    ">>实际参数（调用函数）\n",
    ">>有多个参数时：\n",
    "\n",
    ">>>关键字参数\n",
    ">>>位置参数（调用函数时）---> 关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>>默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "314122e1",
   "metadata": {},
   "source": [
    "# 本周知识\n",
    "\n",
    "> 1.函数的返回值\n",
    "> 2.函数的输入参数类型\n",
    "> 3.函数的返回参数类型\n",
    "> 4.模块的概念\n",
    "> 5.如何创建模块，并调用自己定义的模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89d2375b",
   "metadata": {},
   "source": [
    "# 函数的返回值\n",
    "> 1.返回的关键字：return\n",
    ">> 1.函数一定有返回值（无论什么语言）\n",
    ">> 2.在其他编程语言中（如Java等），必写返回值，不写会报错！对于Python（等更高级的语言），返回值如果不写，给定默认值None\n",
    ">> 3.返回值一般是函数体运行的结果（result）（即函数返回值实践1）；也可自定义的函数返回值（实践2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "77e81410",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formattered_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + '' + last_name\n",
    "# print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bd4a0a87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LiHua'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formattered_name(\"Li\",\"Hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a4e88a67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LiHua\n"
     ]
    }
   ],
   "source": [
    "print(get_formattered_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f3b261e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8fb09644",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10a609b3",
   "metadata": {},
   "source": [
    "## 2.函数的输入参数类型\n",
    "\n",
    "> 1.函数在调用过程中可以输入复杂的参数：(例如列表,字典,集合等)\n",
    "\n",
    ">> 1.函数参数为列表\n",
    ">> 2.函数参数为字典\n",
    ">> 3. ...\n",
    "\n",
    "> 2.函数可以输入不确定个数的参数\n",
    ">> 1.*args\n",
    ">> 2.**kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6aa11718",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试函数输入一个列表\"\"\"\n",
    "    return names\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7800b272",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Zico']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A([\"Lihua\",\"Alex\",\"Zico\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3c0b572c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数参数输入值为字典\"\"\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6c5e0bfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stud_id\":210001,\"age\":18,\"Job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c4be807",
   "metadata": {},
   "source": [
    "> 2.函数可以输入不确定个数的参数\n",
    ">> 1.*args\n",
    ">>> 1.args 可以接受多个数据（单值）\n",
    ">>> 2.args 将多个数据存在一个**元组**当中，实际还是一个变量\n",
    ">> 2.**kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "60e7efb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "cada6beb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers'))"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\",\"green peppers\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "aaa596bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def person(stud_name,**stud_course):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stud_name,stud_course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0c53d9f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Luhan', {'course_星期一': 'API', 'course_星期二': 'APP原型设计'})"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"Luhan\",course_星期一=\"API\",course_星期二=\"APP原型设计\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f708e57",
   "metadata": {},
   "source": [
    "#  函数的输出（返回值）参数类型\n",
    "\n",
    "> 1.在编程语言中，函数返回值只可以是一个值\n",
    "> 2.返回值可以是 字符串、数值、列表、字典等数据（单一值）\n",
    "> 3.python可以输出多个返回值，结果会用一个元组进行封装并返回。（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1886d227",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2c0da19b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2212f651",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为数值\"\"\"\n",
    "    return 12345"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "73227e81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4ef10ed2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return ['Lihua', 'Alex', 'Ziqi']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "606fbad3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "523a0006",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字典\"\"\"\n",
    "    return {'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f84ef500",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "625d3e58",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ******** return 后面写多个值，将会返回一个元组（一个值，不违背函数在编程思维中的定义）\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 18,\"Lihua\",['Lihua', 'Alex', 'Ziqi'],{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f340a8f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18,\n",
       " 'Lihua',\n",
       " ['Lihua', 'Alex', 'Ziqi'],\n",
       " {'stud_id': 210001, 'age': 18, 'Job': 'student'})"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "48d97e8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test6_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 88,89,90,90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f0809475",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(88, 89, 90, 90)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test6_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d81807c",
   "metadata": {},
   "source": [
    "# 模块\n",
    "> 1.使用模块可以隐藏函数的源代码\n",
    "> 2.使用的人不需要会底层代码（只需要会使用模块即可）\n",
    "\n",
    "> 3.模块的使用的方法：\n",
    ">> *import 模块名\n",
    "使用： 模块名.函数名()\n",
    "\n",
    ">> *from 模块名 import 函数名\n",
    "使用： 函数名()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "354057ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "96223807",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=22, tm_hour=9, tm_min=43, tm_sec=35, tm_wday=2, tm_yday=356, tm_isdst=0)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2a6f92ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import localtime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a2c7de95",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=22, tm_hour=9, tm_min=43, tm_sec=36, tm_wday=2, tm_yday=356, tm_isdst=0)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "34b72cc4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'study'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-52-ab8b37a5fa59>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mstudy\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'study'"
     ]
    }
   ],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "63ce3e64",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'study' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-53-07073a8e0599>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 函数的调用\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch4vowels\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hello zhichao\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'study' is not defined"
     ]
    }
   ],
   "source": [
    "# 函数的调用\n",
    "study.search4vowels(\"hello zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "129eac68",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'study' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-54-7c00a969a59c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'study' is not defined"
     ]
    }
   ],
   "source": [
    "help(study.news_链接)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "b0eb2ca2",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'study' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-55-14569224e1d2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'https://www.nfu.edu.cn/'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'xydt'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'98'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'study' is not defined"
     ]
    }
   ],
   "source": [
    "study.news_链接('https://www.nfu.edu.cn/','xydt','98')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25b4ddbc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2539507c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
